// src/store/user.ts
import { defineStore } from "pinia"
import type { User } from "@/types/login"

const useUserStore = defineStore({
	id: "userStore",
	state: () => {
		return {
			appId: uni.getAccountInfoSync().miniProgram.appId,
			openId: uni.getStorageSync("open_id") || "oFuN55GjrAFOBbEHVuUF6I082yPs",
			token: uni.getStorageSync("token") || "",
			user: uni.getStorageSync("user") || ({} as User),
			longitude: 0, // 经度
			latitude: 0, // 纬度
		}
	},
	actions: {
		// 更新openid
		updateOpenId(payload: string) {
			this.openId = payload
			uni.setStorageSync("open_id", payload)
		},
		// 更新登陆信息
		updateUser(payload: any) {
			this.token = payload["token"]
			this.user = payload["user"]
			uni.setStorageSync("token", this.token)
			uni.setStorageSync("user", this.user)
		},
		updateLocation(payload: any) {
			this.longitude = payload.longitude
			this.latitude = payload.latitude
		},
	},
	persist: {
		key: "userStore", // 存储在 localStorage 或 sessionStorage 中的 key
		storage: localStorage, // 指定存储方式，默认为 localStorage
	},
})

export default useUserStore
